package com.btpj.hospital_server.controller;

import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.param.HospitalClinicParam;
import com.btpj.hospital_server.entity.HospitalClinic;
import com.btpj.hospital_server.service.HospitalClinicService;
import com.btpj.hospital_server.service.HospitalOutpatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 医院诊室Controller
 *
 * @author BTPJ  2020/8/27
 */
@Api(tags = "医院诊室接口")
@RestController
@RequestMapping("/clinic")
public class HospitalClinicController {

    @Resource
    private HospitalOutpatientService hospitalOutpatientService;

    @Resource
    private HospitalClinicService hospitalClinicService;

    @ApiOperation("添加诊室信息")
    @ApiImplicitParam(name = "outpatientId", value = "所属门诊编号", required = true, dataType = "Long", paramType = "query")
    @PostMapping("/add")
    public CommonResult<Void> addClinic(HospitalClinicParam param) {
        if (!hospitalOutpatientService.existOutpatient(param.getOutpatientId())) {
            return CommonResult.error("诊室所属的门诊编号不存在");
        }
        if (hospitalClinicService.addClinic(param)) {
            return CommonResult.success("添加诊室信息成功");
        }

        return CommonResult.error("服务器错误，添加诊室信息失败");
    }

    @ApiOperation("修改诊室信息")
    @ApiImplicitParam(name = "id", value = "诊室编号", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/update/{id}")
    public CommonResult<Void> updateClinic(@PathVariable Long id, HospitalClinicParam param) {
        if (!hospitalClinicService.existClinic(id)) {
            return CommonResult.error("不存在指定编号的诊室");
        }

        if (param.getOutpatientId() != null && !hospitalOutpatientService.existOutpatient(param.getOutpatientId())) {
            return CommonResult.error("诊室所属的门诊编号不存在");
        }

        if (hospitalClinicService.updateClinic(id, param)) {
            return CommonResult.success("修改诊室信息成功");
        }

        return CommonResult.error("服务器错误，修改诊室信息失败");
    }

    @ApiOperation("删除诊室信息")
    @ApiImplicitParam(name = "id", value = "诊室编号", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> updateClinic(@PathVariable Long id) {
        if (!hospitalClinicService.existClinic(id)) {
            return CommonResult.error("不存在指定编号的诊室");
        }

        if (hospitalClinicService.deleteClinic(id)) {
            return CommonResult.success("删除诊室信息成功");
        }

        return CommonResult.error("服务器错误，删除诊室信息失败");
    }

    @ApiOperation("获取诊室信息")
    @ApiImplicitParam(name = "id", value = "诊室编号", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/detail/{id}")
    public CommonResult<HospitalClinic> getClinicDetails(@PathVariable Long id) {
        if (!hospitalClinicService.existClinic(id)) {
            return CommonResult.error("不存在指定编号的诊室");
        }

        HospitalClinic clinic = hospitalClinicService.getClinicDetails(id);
        if (clinic != null) {
            return CommonResult.success(clinic);
        }

        return CommonResult.error("服务器错误，获取诊室信息失败");
    }
}
